package com.jwsoft.manager.core.integration.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bifang.engine.assistant.core.annotation.ApiService;
import com.bifang.engine.assistant.core.annotation.OpApi;
import com.bifang.engine.assistant.core.annotation.SaveOplog;
import com.bifang.engine.assistant.core.enums.AuTypeEnum;
import com.bifang.engine.assistant.core.enums.BoolEnum;
import com.bifang.engine.assistant.core.enums.FuncTypeEnum;
import com.bifang.engine.assistant.core.exception.AppException;
import com.bifang.engine.data.core.util.PagerUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jwsoft.manager.common.vo.eduStudentEnrollInvalid.EduStudentEnrollInvalidKeyVO;
import com.jwsoft.manager.common.vo.eduStudentEnrollInvalid.EduStudentEnrollInvalidQueryVO;
import com.jwsoft.manager.common.vo.eduStudentEnrollInvalid.EduStudentEnrollInvalidVO;
import com.jwsoft.manager.core.dao.model.EduStudentEnrollInvalid;
import com.jwsoft.manager.core.dao.service.EduStudentEnrollInvalidService;
import com.jwsoft.manager.core.integration.EduStudentEnrollInvalidIntegration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.List;
/**
 * eduStudentEnrollInvalid业务实现类
 *
 * @author wangtao
 * @since 2022-12-28
 */
@ApiService(funcCode = "eduStudentEnrollInvalid", title = "eduStudentEnrollInvalid")
public class EduStudentEnrollInvalidIntegrationImpl implements EduStudentEnrollInvalidIntegration {

    @Autowired
    public EduStudentEnrollInvalidService eduStudentEnrollInvalidService;

    @Override
    @OpApi(funcCode = "eduStudentEnrollInvalid0001", title = "eduStudentEnrollInvalid分页查询", funcType = FuncTypeEnum.query)
    public PageInfo<EduStudentEnrollInvalidVO> getList(EduStudentEnrollInvalidQueryVO vo) {
        // 开启分页
        if (vo.getPageNum() == null || vo.getPageSize() == null) {
            //如果不传入分页参数，默认最大取1000条
            vo.setPageNum(1);
            vo.setPageSize(1000);
        }
        PageHelper.startPage(vo.getPageNum(),vo.getPageSize());
        List<EduStudentEnrollInvalid> list = eduStudentEnrollInvalidService.list();
        if (CollectionUtils.isEmpty(list)) {
            // 判断是否为空，为空则返回空参数
            return PagerUtil.createEmptyPagerInfo(vo.getPageNum(), vo.getPageSize());
        }
        // 不为空则获取分页结果
        PageInfo<EduStudentEnrollInvalid> pageInfo = new PageInfo<>(list);
        // 返回参数需要通过common层的vo实体类，用该方法可以批量转换
        List<EduStudentEnrollInvalidVO> resultList = Convert.toList(EduStudentEnrollInvalidVO.class, pageInfo.getList());
        // 转义数据字典值
        // dictHelper.valueToName(resultList, Arrays.asList(DemoDictEnum.values()));
        return PagerUtil.parsePagerVo(resultList, pageInfo);
    }

    @Override
    @OpApi(funcCode = "eduStudentEnrollInvalid0002", title = "eduStudentEnrollInvalid根据主键查询详情", funcType = FuncTypeEnum.query)
    public EduStudentEnrollInvalidVO getById(EduStudentEnrollInvalidKeyVO vo) {
        EduStudentEnrollInvalid entity = eduStudentEnrollInvalidService.getById(vo.getStudentInvalidId());
        if (ObjectUtils.isEmpty(entity)) {
            throw new AppException("查询不到详细信息!");
        }
        EduStudentEnrollInvalidVO result = Convert.convert(EduStudentEnrollInvalidVO.class, entity);
        return result;
    }
    /** 保存前统一校验*/
    @Override
    @OpApi(funcCode = "eduStudentEnrollInvalid0003", title = "eduStudentEnrollInvalid保存前校验", funcType = FuncTypeEnum.other ,checkPrivilege = BoolEnum.FALSE)
    public void checkSave(EduStudentEnrollInvalidVO vo) {
        //主键为空为新增校验

        //主键不为空为修改校验
    }

    @Override
    @OpApi(funcCode = "eduStudentEnrollInvalid0004", title = "eduStudentEnrollInvalid新增", funcType = FuncTypeEnum.insert)
    @SaveOplog(
            operationType = "eduStudentEnrollInvalid0004",
            operationName = "eduStudentEnrollInvalid新增",
            dataType = "studentInvalidId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public EduStudentEnrollInvalidVO add(EduStudentEnrollInvalidVO vo) {
        //保存前校验
        checkSave(vo);
        //保存数据
        EduStudentEnrollInvalid studentEnrollInvalid = Convert.convert(EduStudentEnrollInvalid.class, vo);
        LambdaQueryWrapper<EduStudentEnrollInvalid> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EduStudentEnrollInvalid::getStudentEnrollId,vo.getStudentEnrollId())
                    .eq(EduStudentEnrollInvalid::getDel,"0");
        EduStudentEnrollInvalid enrollInvalid = eduStudentEnrollInvalidService.getOne(queryWrapper);
        if(enrollInvalid != null){
            throw new AppException("该学生的录取作废申请已经提交，请勿重复操作！");
        }
        studentEnrollInvalid.setStudentInvalidId(IdUtil.simpleUUID());
        studentEnrollInvalid.setAuthStatus("0");
        eduStudentEnrollInvalidService.save(studentEnrollInvalid);
        //将主键设置回vo使操作日志可以取到
        vo.setStudentInvalidId(studentEnrollInvalid.getStudentInvalidId());
        return vo;
    }

    @Override
    @OpApi(funcCode = "eduStudentEnrollInvalid0005", title = "eduStudentEnrollInvalid修改", funcType = FuncTypeEnum.update)
    @SaveOplog(
            operationType = "eduStudentEnrollInvalid0005",
            operationName = "eduStudentEnrollInvalid修改",
            dataType = "studentInvalidId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public EduStudentEnrollInvalidVO edit(EduStudentEnrollInvalidVO vo) {
        if (ObjectUtils.isEmpty(vo.getStudentInvalidId())) {
            throw new AppException("传入需要修改的主键不能为空!");
        }
        EduStudentEnrollInvalid oldEntity = eduStudentEnrollInvalidService.getById(vo.getStudentInvalidId());
        if (ObjectUtils.isEmpty(oldEntity)) {
            throw new AppException("查询不到需要修改的信息!");
        }
        //保存前校验
        checkSave(vo);
        //保存数据
        EduStudentEnrollInvalid entity = Convert.convert(EduStudentEnrollInvalid.class, vo);
        eduStudentEnrollInvalidService.updateById(entity);
        return vo;
    }

    @Override
    @OpApi(funcCode = "eduStudentEnrollInvalid0006", title = "eduStudentEnrollInvalid根据主键删除", funcType = FuncTypeEnum.delete)
    @SaveOplog(
            operationType = "eduStudentEnrollInvalid0006",
            operationName = "eduStudentEnrollInvalid删除",
            dataType = "studentInvalidId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public void del(EduStudentEnrollInvalidKeyVO vo) {
        if (ObjectUtils.isEmpty(vo.getStudentInvalidId())) {
            throw new AppException("传入需要删除的数据主键不能为空!");
        }
        EduStudentEnrollInvalid entity = eduStudentEnrollInvalidService.getById(vo.getStudentInvalidId());
        if (ObjectUtils.isEmpty(entity)) {
            throw new AppException("未找到需要删除的信息!");
        }
        eduStudentEnrollInvalidService.removeById(vo.getStudentInvalidId());
    }
}
